package com.zsj.service_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zsj.service_user.entity.AppUser;
import com.zsj.service_user.entity.pojo.AvatarName;
import com.zsj.service_user.mapper.AppUserMapper;
import com.zsj.service_user.service.AppUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsj.service_user.util.DoPass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zsj
 * @since 2022-04-27
 */
@Service
@Slf4j
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {

    @Autowired
    private AppUserService appUserService;

    /**
     * 用户注册 简单进行密码加密
     *
     * @param user 传入的用户信息
     * @return 返回注册结果
     */
    @Override
    public Boolean registerUser(AppUser user) {
        String pa = DoPass.doPass(user.getUserPassword());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(new Date());
        user.setCreateTime(time);
        user.setUserPassword(pa);
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", user.getUserName());
        int count = appUserService.count(queryWrapper);
        if (count > 0) {
            return false;
        } else
            return appUserService.save(user);
    }

    /**
     * @return 判断数据库中是否含有该用户
     */
    @Override
    public Boolean haveSomeOne(String name, String pass) {
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", name);
        int count = appUserService.count(queryWrapper);
        if (count > 0) {
            queryWrapper.eq("user_password", DoPass.doPass(pass));
            return appUserService.count(queryWrapper) > 0;
        } else return false;
    }

    /**
     * 根据账号名称来修改头像
     *
     * @param info 用户信息
     * @return 修改成功或失败
     */
    @Override
    public boolean updateAvatarByName(AvatarName info) {
        String name = info.getName();
        String url = info.getUrl();
        UpdateWrapper<AppUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("user_avatar", url);
        updateWrapper.eq("user_name", name);
        return appUserService.update(new AppUser(), updateWrapper);
    }

    /**
     * 根据用户名来查询用户头像
     *
     * @param name 用户账号名
     * @return 返回用户头像地址
     */
    @Override
    public String getUserAvatarByName(String name) {
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", name);
        AppUser one = appUserService.getOne(queryWrapper);
        return one.getUserAvatar();
    }


    /**
     * 根据用户账号来查询用户余额
     *
     * @param name 用户账号名
     * @return 用户余额
     */
    @Override
    public Integer getBalance(String name) {
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_balance");
        queryWrapper.eq("user_name", name);
        AppUser one = appUserService.getOne(queryWrapper);
        log.info("查询到的结果为{}", one.getUserBalance());
        return Integer.valueOf(one.getUserBalance());
    }

    /**
     * 修改用户余额
     *
     * @param name  用户账号名
     * @param price 充值的金额
     * @return 返回充值结果
     */
    @Override
    public boolean addBalance(String name, Integer price) {
        UpdateWrapper<AppUser> wrapper = new UpdateWrapper<>();
        Integer balance = appUserService.getBalance(name);
        wrapper.set("user_balance", balance + price);
        wrapper.eq("user_name", name);
        return appUserService.update(new AppUser(), wrapper);
    }

    /**
     * 修改用户个性名称
     * @param name  账号名
     * @param newName 新的个性名称
     * @return  返回是否修改成功
     */
    @Override
    public boolean upNickName(String name, String newName) {
        UpdateWrapper<AppUser> wrapper = new UpdateWrapper<>();
        wrapper.set("user_nickName", newName);
        wrapper.eq("user_name", name);
        return appUserService.update(new AppUser(), wrapper);
    }

    /**
     * 修改用户性别
     * @param name  账号名
     * @param newSex 新的性别
     * @return  返回是否修改成功
     */
    @Override
    public boolean upSex(String name, String newSex) {
        UpdateWrapper<AppUser> wrapper = new UpdateWrapper<>();
        wrapper.set("user_sex", newSex);
        wrapper.eq("user_name", name);
        return appUserService.update(new AppUser(), wrapper);

    }
    /**
     * 修改用户生日
     * @param name  账号名
     * @param newBirthday 新的生日
     * @return  返回是否修改成功
     */
    @Override
    public boolean upBirthday(String name, String newBirthday) {
        UpdateWrapper<AppUser> wrapper = new UpdateWrapper<>();
        wrapper.set("user_birthday",newBirthday);
        wrapper.eq("user_name",name);
        return appUserService.update(new AppUser(),wrapper);
    }
    /**
     * 修改用户密码
     * @param name  账号名
     * @param oldPass 旧密码
     * @param newPass 新的密码
     * @return  返回是否修改成功
     */
    @Override
    public boolean upPassword(String name, String oldPass, String newPass) {
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",name);
        AppUser user = appUserService.getOne(queryWrapper);
        String real = user.getUserPassword().substring(0, oldPass.length());
        if (real.equals(oldPass)){
            //说明他输入的旧密码是正确的
            UpdateWrapper<AppUser> userUpdateWrapper =new UpdateWrapper<>();
            newPass = DoPass.doPass(newPass);
            userUpdateWrapper.set("user_password",newPass);
            userUpdateWrapper.eq("user_name",name);
            return appUserService.update(new AppUser(),userUpdateWrapper);
        }else return false;
    }


}
